<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>搜索 | 数据结构与算法（Python）</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter7/index.html" />
    
    
    <link rel="prev" href="../chapter6/section7.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.8"
        data-chapter-title="搜索"
        data-filepath="chapter6/section8.md"
        data-basepath=".."
        data-revision="Tue May 23 2017 16:07:52 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        数据结构与算法（Python）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/index.html">
            
                
                    <a href="../chapter1/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入概念
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/section1.html">
            
                
                    <a href="../chapter1/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        第一次尝试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/section2.html">
            
                
                    <a href="../chapter1/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        算法的提出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/section3.html">
            
                
                    <a href="../chapter1/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        第二次尝试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/section4.html">
            
                
                    <a href="../chapter1/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        算法效率衡量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/section5.html">
            
                
                    <a href="../chapter1/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        算法分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/section6.html">
            
                
                    <a href="../chapter1/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        常见时间复杂度
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="chapter1/section7.html">
            
                
                    <a href="../chapter1/section7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        Python内置类型性能分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="chapter1/section8.html">
            
                
                    <a href="../chapter1/section8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        数据结构
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/index.html">
            
                
                    <a href="../chapter2/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        顺序表
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/section1.html">
            
                
                    <a href="../chapter2/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        顺序表的形式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/section2.html">
            
                
                    <a href="../chapter2/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        顺序表的结构与实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/section3.html">
            
                
                    <a href="../chapter2/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        顺序表的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="chapter2/section4.html">
            
                
                    <a href="../chapter2/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        Python中的顺序表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/index.html">
            
                
                    <a href="../chapter3/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        链表
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/section1.html">
            
                
                    <a href="../chapter3/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        单向链表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/section3.html">
            
                
                    <a href="../chapter3/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        双向链表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/section2.html">
            
                
                    <a href="../chapter3/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        单向循环链表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter4/index.html">
            
                
                    <a href="../chapter4/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        栈
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="chapter4/section1.html">
            
                
                    <a href="../chapter4/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        栈结构实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter5/index.html">
            
                
                    <a href="../chapter5/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        队列
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="chapter5/section1.html">
            
                
                    <a href="../chapter5/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        队列的实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="chapter5/section3.html">
            
                
                    <a href="../chapter5/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        双端队列
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/index.html">
            
                
                    <a href="../chapter6/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排序与搜索
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="chapter6/section1.html">
            
                
                    <a href="../chapter6/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        冒泡排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="chapter6/section2.html">
            
                
                    <a href="../chapter6/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        选择排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="chapter6/section3.html">
            
                
                    <a href="../chapter6/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        插入排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="chapter6/section4.html">
            
                
                    <a href="../chapter6/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        快速排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="chapter6/section5.html">
            
                
                    <a href="../chapter6/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        希尔排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="chapter6/section6.html">
            
                
                    <a href="../chapter6/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        归并排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="chapter6/section7.html">
            
                
                    <a href="../chapter6/section7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        常见排序算法效率比较
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.8" data-path="chapter6/section8.html">
            
                
                    <a href="../chapter6/section8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        搜索
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter7/index.html">
            
                
                    <a href="../chapter7/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        树与树算法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="chapter7/section1.html">
            
                
                    <a href="../chapter7/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        二叉树
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="chapter7/section2.html">
            
                
                    <a href="../chapter7/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        二叉树的遍历
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >数据结构与算法（Python）</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x641C;&#x7D22;">&#x641C;&#x7D22;</h1>
<p>&#x641C;&#x7D22;&#x662F;&#x5728;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#x96C6;&#x5408;&#x4E2D;&#x627E;&#x5230;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x9879;&#x76EE;&#x7684;&#x7B97;&#x6CD5;&#x8FC7;&#x7A0B;&#x3002;&#x641C;&#x7D22;&#x901A;&#x5E38;&#x7684;&#x7B54;&#x6848;&#x662F;&#x771F;&#x7684;&#x6216;&#x5047;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x8BE5;&#x9879;&#x76EE;&#x662F;&#x5426;&#x5B58;&#x5728;&#x3002;
&#x641C;&#x7D22;&#x7684;&#x51E0;&#x79CD;&#x5E38;&#x89C1;&#x65B9;&#x6CD5;&#xFF1A;&#x987A;&#x5E8F;&#x67E5;&#x627E;&#x3001;&#x4E8C;&#x5206;&#x6CD5;&#x67E5;&#x627E;&#x3001;&#x4E8C;&#x53C9;&#x6811;&#x67E5;&#x627E;&#x3001;&#x54C8;&#x5E0C;&#x67E5;&#x627E;</p>
<h2 id="&#x4E8C;&#x5206;&#x6CD5;&#x67E5;&#x627E;">&#x4E8C;&#x5206;&#x6CD5;&#x67E5;&#x627E;</h2>
<p>&#x4E8C;&#x5206;&#x67E5;&#x627E;&#x53C8;&#x79F0;&#x6298;&#x534A;&#x67E5;&#x627E;&#xFF0C;&#x4F18;&#x70B9;&#x662F;&#x6BD4;&#x8F83;&#x6B21;&#x6570;&#x5C11;&#xFF0C;&#x67E5;&#x627E;&#x901F;&#x5EA6;&#x5FEB;&#xFF0C;&#x5E73;&#x5747;&#x6027;&#x80FD;&#x597D;&#xFF1B;&#x5176;&#x7F3A;&#x70B9;&#x662F;&#x8981;&#x6C42;&#x5F85;&#x67E5;&#x8868;&#x4E3A;&#x6709;&#x5E8F;&#x8868;&#xFF0C;&#x4E14;&#x63D2;&#x5165;&#x5220;&#x9664;&#x56F0;&#x96BE;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6298;&#x534A;&#x67E5;&#x627E;&#x65B9;&#x6CD5;&#x9002;&#x7528;&#x4E8E;&#x4E0D;&#x7ECF;&#x5E38;&#x53D8;&#x52A8;&#x800C;&#x67E5;&#x627E;&#x9891;&#x7E41;&#x7684;&#x6709;&#x5E8F;&#x5217;&#x8868;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x5047;&#x8BBE;&#x8868;&#x4E2D;&#x5143;&#x7D20;&#x662F;&#x6309;&#x5347;&#x5E8F;&#x6392;&#x5217;&#xFF0C;&#x5C06;&#x8868;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;&#x8BB0;&#x5F55;&#x7684;&#x5173;&#x952E;&#x5B57;&#x4E0E;&#x67E5;&#x627E;&#x5173;&#x952E;&#x5B57;&#x6BD4;&#x8F83;&#xFF0C;&#x5982;&#x679C;&#x4E24;&#x8005;&#x76F8;&#x7B49;&#xFF0C;&#x5219;&#x67E5;&#x627E;&#x6210;&#x529F;&#xFF1B;&#x5426;&#x5219;&#x5229;&#x7528;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;&#x8BB0;&#x5F55;&#x5C06;&#x8868;&#x5206;&#x6210;&#x524D;&#x3001;&#x540E;&#x4E24;&#x4E2A;&#x5B50;&#x8868;&#xFF0C;&#x5982;&#x679C;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;&#x8BB0;&#x5F55;&#x7684;&#x5173;&#x952E;&#x5B57;&#x5927;&#x4E8E;&#x67E5;&#x627E;&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5219;&#x8FDB;&#x4E00;&#x6B65;&#x67E5;&#x627E;&#x524D;&#x4E00;&#x5B50;&#x8868;&#xFF0C;&#x5426;&#x5219;&#x8FDB;&#x4E00;&#x6B65;&#x67E5;&#x627E;&#x540E;&#x4E00;&#x5B50;&#x8868;&#x3002;&#x91CD;&#x590D;&#x4EE5;&#x4E0A;&#x8FC7;&#x7A0B;&#xFF0C;&#x76F4;&#x5230;&#x627E;&#x5230;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x7684;&#x8BB0;&#x5F55;&#xFF0C;&#x4F7F;&#x67E5;&#x627E;&#x6210;&#x529F;&#xFF0C;&#x6216;&#x76F4;&#x5230;&#x5B50;&#x8868;&#x4E0D;&#x5B58;&#x5728;&#x4E3A;&#x6B62;&#xFF0C;&#x6B64;&#x65F6;&#x67E5;&#x627E;&#x4E0D;&#x6210;&#x529F;&#x3002;</p>
<p><img src="../images/Binary_search_into_array.png" alt="Binary_search_into_array"></p>
<h2 id="&#x4E8C;&#x5206;&#x6CD5;&#x67E5;&#x627E;&#x5B9E;&#x73B0;">&#x4E8C;&#x5206;&#x6CD5;&#x67E5;&#x627E;&#x5B9E;&#x73B0;</h2>
<h3 id="&#xFF08;&#x975E;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#xFF09;">&#xFF08;&#x975E;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#xFF09;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">binary_search</span><span class="hljs-params">(alist, item)</span>:</span>
      first = <span class="hljs-number">0</span>
      last = len(alist)-<span class="hljs-number">1</span>
      <span class="hljs-keyword">while</span> first&lt;=last:
          midpoint = (first + last)/<span class="hljs-number">2</span>
          <span class="hljs-keyword">if</span> alist[midpoint] == item:
              <span class="hljs-keyword">return</span> <span class="hljs-keyword">True</span>
          <span class="hljs-keyword">elif</span> item &lt; alist[midpoint]:
              last = midpoint-<span class="hljs-number">1</span>
          <span class="hljs-keyword">else</span>:
              first = midpoint+<span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span>
testlist = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">13</span>, <span class="hljs-number">17</span>, <span class="hljs-number">19</span>, <span class="hljs-number">32</span>, <span class="hljs-number">42</span>,]
print(binary_search(testlist, <span class="hljs-number">3</span>))
print(binary_search(testlist, <span class="hljs-number">13</span>))
</code></pre>
<h3 id="&#xFF08;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#xFF09;">&#xFF08;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#xFF09;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">binary_search</span><span class="hljs-params">(alist, item)</span>:</span>
    <span class="hljs-keyword">if</span> len(alist) == <span class="hljs-number">0</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span>
    <span class="hljs-keyword">else</span>:
        midpoint = len(alist)//<span class="hljs-number">2</span>
        <span class="hljs-keyword">if</span> alist[midpoint]==item:
          <span class="hljs-keyword">return</span> <span class="hljs-keyword">True</span>
        <span class="hljs-keyword">else</span>:
          <span class="hljs-keyword">if</span> item&lt;alist[midpoint]:
            <span class="hljs-keyword">return</span> binary_search(alist[:midpoint],item)
          <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> binary_search(alist[midpoint+<span class="hljs-number">1</span>:],item)

testlist = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">13</span>, <span class="hljs-number">17</span>, <span class="hljs-number">19</span>, <span class="hljs-number">32</span>, <span class="hljs-number">42</span>,]
print(binary_search(testlist, <span class="hljs-number">3</span>))
print(binary_search(testlist, <span class="hljs-number">13</span>))
</code></pre>
<h3 id="&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;">&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;</h3>
<ul>
<li>&#x6700;&#x4F18;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#xFF1A;O(1)</li>
<li>&#x6700;&#x574F;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#xFF1A;O(logn)</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter6/section7.html" class="navigation navigation-prev " aria-label="Previous page: 常见排序算法效率比较"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter7/index.html" class="navigation navigation-next " aria-label="Next page: 树与树算法"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
